Skip to content

Conversation

@shenyih0ng
Copy link
Contributor

@shenyih0ng shenyih0ng commented Mar 29, 2025

Fixes #18790

This PR adds a new override check to prevent instance properties (defined using @property) from overriding inherited class variables.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@shenyih0ng shenyih0ng force-pushed the fix/prevent-classvar-override-with-property branch from c47da59 to 1486fad Compare March 29, 2025 16:44
@shenyih0ng shenyih0ng force-pushed the fix/prevent-classvar-override-with-property branch from 1486fad to f2f8a98 Compare March 29, 2025 16:55
Comment on lines +2297 to +2300
self.fail(
message_registry.CANNOT_OVERRIDE_CLASS_VAR.format(base.name),
decorator_func,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if the error code OVERRIDE should be attached to this error message? left it as misc to keep behaviour consistent with existing error message:

self.fail(message_registry.CANNOT_OVERRIDE_CLASS_VAR.format(base.name), node)

@shenyih0ng shenyih0ng marked this pull request as ready for review March 29, 2025 17:20
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

django-stubs (https://github.com/typeddjango/django-stubs)
+ django-stubs/db/migrations/operations/special.pyi:38: error: Cannot override class variable (previously declared on base class "Operation") with instance variable  [misc]
+ django-stubs/db/migrations/operations/special.pyi:38: note: Error code "misc" not covered by "type: ignore" comment
+ django-stubs/db/migrations/operations/special.pyi:59: error: Cannot override class variable (previously declared on base class "Operation") with instance variable  [misc]
+ django-stubs/db/migrations/operations/special.pyi:59: note: Error code "misc" not covered by "type: ignore" comment

from typing import ClassVar

class A:
x: ClassVar[int]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a testcase with ClassVar[property]? I suspect that isn't supported even without this PR (#18504) and don't know if it is important enough, but better be explicit about this.

class Parent:
   foo: ClassVar[property]

class Child(Parent):
    @property
    def foo(self) -> None: ...
    @foo.setter
    def foo(self, _: None) -> None: ...

(this should ideally be accepted, but probably isn't yet)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

does not prevent overriding of ClassVar with instance setter

2 participants